package com.chookss.home.other;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.adapter.NavigationAdapter;
import com.chookss.home.entity.NavigationEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MoreFunctionActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.rvView)
    RecyclerView rvView;
    @BindView(R.id.srlView)
    SmartRefreshLayout srlView;
    @BindView(R.id.ivNone)
    ImageView ivNone;
    @BindView(R.id.tvNone)
    TextView tvNone;
    @BindView(R.id.llNone)
    LinearLayout llNone;
    @BindView(R.id.llBg)
    LinearLayout llBg;
    private NavigationAdapter navigationAdapter;
    private ArrayList<NavigationEntity> navigationEntityList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_srlview_center);
        ButterKnife.bind(this);
        initState();
    }

    @Override
    public void initData() {
        llBg.setBackgroundColor(getResources().getColor(R.color.white));
        srlView.setPadding(0, Utils.dip2px(this,20),0,0);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        rvView.setLayoutManager(gridLayoutManager);
        navigationAdapter = new NavigationAdapter(R.layout.item_home_navigation, this, navigationEntityList);
        rvView.setAdapter(navigationAdapter);
        srlView.setEnableRefresh(false);
        srlView.setEnableLoadMore(false);
        ivNone.setBackgroundResource(R.drawable.icon_default_no_common);
        tvNone.setText("暂无分类");
        commonTitleTxt.setText("全部分类");
        showLoading();
        getNavigations();
    }

    public void getNavigations() {
        //获取导航
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<NavigationEntity>>>() {
        }.getType(), null, Urls.selectByRole, SignUtils2.makeSignStr(null), false, new ObserverCallback<List<NavigationEntity>>() {
            @Override
            public void onSuccess(List<NavigationEntity> data) {
                dismissLoading();
                if (data.size() > 0) {
                    navigationEntityList.clear();
                    navigationEntityList.addAll(NavigationTools.getNavigation(MoreFunctionActivity.this,data,0));
                    navigationAdapter.notifyDataSetChanged();
                } else {
                    llNone.setVisibility(View.VISIBLE);
                    rvView.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(String errorMsg) {
                dismissLoading();
                MyToast.show(errorMsg);
            }
        });

    }

    @OnClick(R.id.common_title_back)
    public void onClick() {
        finish();
    }


}
